//
//  DatabaseHandle.m
//  UI_豆瓣
//
//  Created by WangZe on 15/10/14.
//  Copyright © 2015年 王泽龙. All rights reserved.
//

#import "DatabaseHandle.h"

@implementation DatabaseHandle

+ (instancetype)shareDatabaseHandle
{
    static DatabaseHandle *handle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DatabaseHandle alloc] init];
    });
    
    return handle;
    
}

- (void)openDB
{
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"activity.sqlite"];
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"开启成功");
        NSLog(@"%@", dbPath);
    }
    else
    {
        NSLog(@"开启失败");
    }
}
- (void)createTable
{
    NSString *sqlStr = @"create table if not exists activities (title text, begin_time text, address text, category_name text, wisher_count text, participant_count text, image text, end_time text, adapt_url text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }
    else
    {
        NSLog(@"创建失败");
    }
    
}
- (void)insertActivity:(ActivityList *)activity
{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into activities (title, begin_time, address, category_name, wisher_count, participant_count, image,end_time, adapt_url) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", activity.title, activity.begin_time, activity.address, activity.category_name, activity.wisher_count, activity.participant_count, activity.image, activity.end_time, activity.adapt_url];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"收藏成功");
    }
    else
    {
        NSLog(@"收藏失败");
    }
}
- (void)deleteActivity:(ActivityList *)activity
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from activities where title = '%@'", activity.title];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除收藏成功");
    }
    else
    {
        NSLog(@"删除收藏失败");
    }
}
- (void)updateActivity:(ActivityList *)activity
{
    NSLog(@"无更新");
}
- (SelectType)isHaveActivity:(ActivityList *)activity
{
    
    NSString *sqlStr = [NSString stringWithFormat: @"select *from activities where title = '%@'", activity.title];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            return InTable;
        }
        else
        {
            sqlite3_finalize(stmt);
            return NotInTable;
        }
    }
    else
    {
        sqlite3_finalize(stmt);
        return SelectError;
    }
    
    
}
- (NSMutableArray *)selectAllActivity
{
    NSString *sqlStr = @"select *from activities";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *actArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 0);
            const unsigned char *begin_time = sqlite3_column_text(stmt, 1);
            const unsigned char * address = sqlite3_column_text(stmt, 2);
            const unsigned char *category_name = sqlite3_column_text(stmt, 3);
            const unsigned char *wisher_count = sqlite3_column_text(stmt, 4);
            const unsigned char *
            participant_count = sqlite3_column_text(stmt, 5);
            const unsigned char *
            image = sqlite3_column_text(stmt, 6);
            const unsigned char * end_time = sqlite3_column_text(stmt, 7);
            const unsigned char * adapt_url = sqlite3_column_text(stmt, 8);
            ActivityList *act = [[ActivityList alloc] init];
            act.title = [NSString stringWithUTF8String:title];
            act.begin_time = [NSString stringWithUTF8String:begin_time];
            act.address = [NSString stringWithUTF8String:address];
            act.category_name = [NSString stringWithUTF8String:category_name];
            act.wisher_count = [NSString stringWithUTF8String:wisher_count];
            act.participant_count = [NSString stringWithUTF8String:participant_count];
            act.image = [NSString stringWithUTF8String:image];
            act.end_time = [NSString stringWithUTF8String:end_time];
            act.adapt_url = [NSString stringWithUTF8String:adapt_url];
            [actArr addObject:act];
            
        }
        
    }//title, begin_time,  address, category_name, wisher_count, participant_count, image,end_time, adapt_url
    sqlite3_finalize(stmt);
    return actArr;
}

@end
